# 在图像处理和计算机视觉应用中，Canny边缘检测是一种很受欢迎的边缘检测方法。
# 为了检测边缘，Canny边缘检测器首先对图像进行平滑处理以降低噪声，然后计算其梯度，然后对梯度应用阈值。
# Canny多阶段边缘检测方法包括以下步骤:
# Gaussian smoothing: The image is smoothed using a Gaussian filter to remove noise.
# Gradient calculation: The gradient of the image is calculated using the Sobel operator.
# Non-maximum suppression: Non-maximum suppression is applied to the gradient image to remove spurious edges.
# Hysteresis thresholding: Hysteresis thresholding is applied to the gradient image to identify strong and weak edges.

# Import the necessary Libraries
import cv2
import numpy as np
import matplotlib.pyplot as plt

# Read image from disk.
img = cv2.imread("cat_dog.jpg")
# Convert BGR image to RGB
image_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# Apply Canny edge detection
edges = cv2.Canny(image=image_rgb, threshold1=100, threshold2=200)

# Create subplots
fig, axs = plt.subplots(1, 2, figsize=(7, 4))

# Plot the original image
axs[0].imshow(image_rgb)
axs[0].set_title("Original Image")

# Plot the blurred image
axs[1].imshow(edges)
axs[1].set_title("Image edges")

# Remove ticks from the subplots
for ax in axs:
    ax.set_xticks([])
    ax.set_yticks([])

# Display the subplots
plt.tight_layout()
plt.show()
